package com.h5;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.cache.CacheSupport;
import com.demo.mobile.MobException;
import com.demo.service.MobileVerifyService;
import com.demo.util.DXWSMSUtils;
import com.demo.util.JSONUtils;
import com.demo.util.ProperityUtil;
import com.demo.util.Sendsms;
import com.vsd.domain.User;
import com.vsd.mobile.error.MEnumError;
import com.vsd.service.StoreService;
import com.vsd.service.UserService;
import com.zy.sys.proto.MOCommon.MRet;

@RequestMapping("/m/login")
@ResponseBody
public class H5LoginController extends H5BaseController {

	@Autowired
	private UserService userService;
	@Autowired
	private ProperityUtil properity;
	@Autowired
	private StoreService storeService;
	@Autowired
	private MobileVerifyService mobileVerifyService;

	@RequestMapping(value="/getCode")
	@ResponseBody
	public String getCode(String phone, Integer type) throws Exception {
		// 查询
		int count = 0;
		Page<User> users = userService
				.findByPhone(phone, new PageRequest(0, 1));
		count = users.getSize();
		
		if (type == 1) {// 注册
			if (count > 0) {
				return JSONUtils.serialize(new Ret(-1,"手机号已存在"));
			}
		} else {
			if (count == 0) {
				return JSONUtils.serialize(new Ret(MEnumError.PHONE_NOTEXISTS_ERROR));
			}
		}
		
		// 发送短信
		int mobileCode = (int) ((Math.random() * 9 + 1) * 100000);
		String content = new String("您的验证码是：" + mobileCode + "。请不要把验证码泄露给其他人。");
		
		// 返回
		Ret ret = new Ret();
		CacheSupport.put("mobileCodeCache", phone, mobileCode + "");
		
//		Sendsms code = DXWSMSUtils.push(phone, content);
//		if(code==null||!code.getCode().equals("0")){
//			CacheSupport.remove("mobileCodeCache", phone);
//			return JSONUtils.serialize(new Ret(-1, code.getMsg()));
//		}
		ret.setCode(0);
		ret.setData(mobileCode+"");
		
		return JSONUtils.serialize(ret);
	}
	
	@RequestMapping(value="checkLogin", method = RequestMethod.POST)
	@ResponseBody
	public String checkLogin(HttpSession session,String account,String password) throws Exception{
		if(StringUtils.isBlank(account) || StringUtils.isBlank(password)){
			return JSONUtils.serialize(new Ret(1, "请填写用户名和密码"));
		}
		
		//校验登录
		User user = userService.getUserByUsernameAndPassword(account, password);
		
		if (user == null){
			return JSONUtils.serialize(new Ret(MEnumError.LOGIN_FAILURE_ERROR));
		}
		
		//禁用
		if(user.getState() == 0){
			return JSONUtils.serialize(new Ret(1, "账号已被封停"));
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", user.getId());
		session.setAttribute(SESSION_H5_USER,map);
		
		// 返回
		return JSONUtils.serialize(new Ret(0));
	}
}
